<?php
namespace app\shop\service\company;
use app\shop\model\company\CompanyTeamClass;
use app\shop\model\company\CompanyTeam;
use app\common\service\Result;
use app\shop\validate\company\Staff as StaffValidate;
class Team
{
    public function classList()
    {
        $app_id = request()->app_id;
        $map = [
            ['app_id', '=', $app_id]
        ];
        $lists = CompanyTeamClass::where($map)->field('id,title,sort')->order('sort desc')->select();
        return Result::success($lists);
    }

    public function addClass($data)
    {
        $data['app_id'] = request()->app_id;
        CompanyTeamClass::create($data);
        return Result::success();
    }

    public function editClass($data)
    {
        $class = CompanyTeamClass::find($data['id']);
        if (!$class) {
            return Result::error('数据不存在');
        }
        $class->save($data);
        return Result::success();
    }

    public function deleteClass($id)
    {
        $class = CompanyTeamClass::find($id);
        if (!$class) {
            return Result::error('数据不存在');
        }
        $class->delete();
        return Result::success();
    }

    public function list($data)
    {
        $app_id = request()->app_id;
        $map = [
            ['app_id', '=', $app_id]
        ];
        if (isset($data['department_id']) && $data['department_id']) {
            $map[] = ['department_id', 'in', $data['department_id']];
        }
        if (isset($data['class_id']) && $data['class_id']) {
            $map[] = ['class_id', '=', $data['class_id']];
        }
        $page_size = $data['page_size'] ?? 20;
        $lists = CompanyTeam::where($map)->with(['teamClass', 'department', 'author'])->order('sort desc,id desc')
            ->paginate($page_size)
            ->bindAttr('teamClass', ['class_name' => 'title'])
            ->bindAttr('department', ['department_name' => 'title'])
            ->bindAttr('author', ['author_name' => 'real_name']);
        return Result::success($lists);
    }


    public function add($data)
    {
        $data['app_id'] = request()->app_id;
        $data['add_user'] = request()->user_id;
        CompanyTeam::create($data);
        return Result::success();
    }

    public function edit($data)
    {
        $info = CompanyTeam::find($data['id']);
        if (!$info) {
            return Result::error('数据不存在');
        }
        $data['add_user'] = request()->user_id;
        $info->save($data);
        return Result::success();
    }

    public function delete($id)
    {
        $info = CompanyTeam::find($id);
        if (!$info) {
            return Result::error('数据不存在');
        }
        $info->delete();
        return Result::success();
    }

    public function info($id)
    {
        $info = CompanyTeam::field('id,title,class_id,department_id,content,sort')->find($id);
        return Result::success($info);
    }

    public function detail($id)
    {
        $info = CompanyTeam::with([
            'teamClass' => function ($query) {
                $query->field('id,title');
            },
            'department' => function ($query) {
                $query->field('id,title');
            },
            'author' => function ($query) {
                $query->field('shop_user_id,real_name');
            },
        ])->find($id);
        return Result::success($info);
    }
}